package Dao;

import Entity.Message;
import Mybatis.Mybatis;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class MessageDao implements MessageDaoImpl {
    public List<Message> findAllMessage() {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.findAllMessage";
        List<Message> messages = session.selectList(statement);
        session.close();
        return messages;
    }
    public Message findById(int id) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.findById";
        Message message = session.selectOne(statement, id);
        session.close();
        return message;
    }
    public List<Message> findBySe_number(String se_number) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.findBySe_number";
        List<Message> messages = session.selectList(statement, se_number);
        session.close();
        return messages;
    }
    public List<Message> findByRe_number(String re_number) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.findByRe_number";
        List<Message> messages = session.selectList(statement, re_number);
        session.close();
        return messages;
    }
    public List<Message> findByStatus(int status) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.findByStatus";
        List<Message> messages = session.selectList(statement, status);
        session.close();
        return messages;
    }
    public List<Message> findByRe_numberANDStatus(String re_number, int status) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        Message message = new Message();
        message.setRe_number(re_number);
        message.setStatus(status);
        String statement = "Mapper.MessageMapper.findByRe_numberANDStatus";
        List<Message> messages = session.selectList(statement, message);
        session.close();
        return messages;
    }
    public boolean addMessage(Message message) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.addMessage";
        int count = session.insert(statement, message);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean updateMessage(Message message) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.updateMessage";
        int count = session.update(statement, message);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean deleteMessage(Message message) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.MessageMapper.deleteMessage";
        int count = session.delete(statement, message);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }

    // ����
    public static void main(String[] args) {
        MessageDao test = new MessageDao();

        List<Message> messages = test.findAllMessage();
        for(int i = 0; i < messages.size(); i++) {
            System.out.println(messages.get(i).getId()+" "+messages.get(i).getMess()+" "+
                    messages.get(i).getSe_number()+" "+messages.get(i).getRe_number()+" "+
                    messages.get(i).getTime()+" "+messages.get(i).getStatus());
        }

    }
}
